﻿using System;
using System.Threading;

namespace csharpbianchang_ch14_parameterizedThreadStart_example2
{
    class Program
    {
        //【实例 2】创建一个方法输出指定范围内数值的偶数，并创建线程调用该方法。
        static void Main(string[] args)
        {
            ParameterizedThreadStart pts = new ParameterizedThreadStart(PrintEven);
            ParametestTest pt = new ParametestTest(1,10);
            Thread t = new Thread(pts);
            t.Start(pt);
        }

        private static void PrintEven(object n)
        {
            //判断n是否为ParameterTest类的对象
            if (n is ParametestTest)
            {
                int beginNum = ((ParametestTest)n).beginNum;
                int endNum = ((ParametestTest)n).endNum;
                for (int i = beginNum; i <= endNum; i++)
                {
                    if (i % 2 == 0)
                    {
                        Console.WriteLine(i);
                    }
                }
            }
        }
    }

    public class ParametestTest//构建两个参数
    {
        public int beginNum;
        public int endNum;
        public ParametestTest(int a, int b)
        {
            this.beginNum = a;
            this.endNum = b;
        }
    }
}
